package cn.dnf.controller;


import cn.dnf.domain.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Date;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/getById/{id}")
    @HystrixCommand(fallbackMethod = "getByIdFallback")
    public User getById(@PathVariable("id")Long id){
        //1.记录时间


        //拿到要调用服务的应用名称之后，就相当于拿到了该服务集群的所有节点IP
        String url = "http://user-server/user/getById/" + id;
        User forObject = restTemplate.getForObject(url, User.class);

        //现在的时间减去刚才的时间

        return forObject;
    }
    //该方法就是降级方法，参数和返回值必须跟熔断接口方法一致，方法名称就是fallbackMethod的名字
    public User getByIdFallback(@PathVariable("id")Long id){
       return new User(-1L,"熔断","服务不能用,稍后重试!");
    }
}
